package com.yuke.cloud.service.uac.controller.frontend;

import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.core.annotation.LogAnnotation;
import com.yuke.cloud.common.core.utils.RequestUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.uac.dto.AccPermissionCheckCodeDto;
import com.yuke.cloud.service.uac.dto.AccPermissionCheckNameDto;
import com.yuke.cloud.service.uac.dto.AccPermissionCheckUrlAndTypeDto;
import com.yuke.cloud.service.uac.dto.PermissionMainQueryDto;
import com.yuke.cloud.service.uac.entity.AccPermission;
import com.yuke.cloud.service.uac.service.AccPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2018/09/15.
*/
@Api(value = "WEB - AccPermissionController", tags = "处理AccPermission相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/acc/permission")
public class AccPermissionController extends BaseController {
    @Resource
    private AccPermissionService accPermissionService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "accPermission", value = "该数据记录的POJO") @RequestBody AccPermission accPermission) {
//        accPermissionService.save(accPermission);
//        return WrapMapper.ok();

        return UpdateOrAddPermission(accPermission);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
//        accPermissionService.deleteById(id);
//        return WrapMapper.ok();

        int result = accPermissionService.deletePermissionById(id);
        return super.handleResult(result);
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "accPermission", value = "需要修改的内容") @RequestBody AccPermission accPermission) {
//        accPermissionService.update(accPermission);
//        return WrapMapper.ok();
        return UpdateOrAddPermission(accPermission);
    }

    private Wrapper UpdateOrAddPermission(AccPermission accPermission) {
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        accPermissionService.savePermission(accPermission, loginAuthDto);
        return WrapMapper.ok(accPermission);
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<AccPermission> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        AccPermission accPermission = accPermissionService.findById(id);
        return WrapMapper.ok(accPermission);
    }

//    @GetMapping
//    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
//    public Wrapper<PageInfo<AccPermission>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
//                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
//        PageHelper.startPage(page, size);
//        List<AccPermission> list = accPermissionService.findAll();
//        PageInfo pageInfo = new PageInfo(list);
//        return WrapMapper.ok(pageInfo);
//    }

    @PostMapping("/getPermissionList")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccPermission>> list(@ApiParam(name = "permissionMainQueryDto", value = "角色信息")
                                                     @RequestBody PermissionMainQueryDto permissionMainQueryDto) {
//        logger.info("查询权限列表actionQuery={}", permissionMainQueryDto);
        PageInfo pageInfo = accPermissionService.queryPermissionListWithPage(permissionMainQueryDto);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 批量删除权限
     *
     * @param deleteIdList the delete id list
     *
     * @return the wrapper
     */
    @PostMapping(value = "/batchDeleteByIdList")
    @ApiOperation(httpMethod = "POST", value = "批量删除权限")
    @LogAnnotation
    public Wrapper batchDeleteByIdList(@ApiParam(name = "deleteIdList", value = "权限Id") @RequestBody List<Long> deleteIdList) {
        logger.info("批量删除权限 idList={}", deleteIdList);
        accPermissionService.batchDeleteByIdList(deleteIdList);
        return WrapMapper.ok();
    }

    /**
     * 检测权限编码是否已存在
     *
     * @param accPermissionCheckCodeDto the acc permission check code dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkCode")
    @ApiOperation(httpMethod = "POST", value = "检测权限编码是否已存在")
    public Wrapper<Boolean> checkActionCode(@ApiParam(name = "accPermissionCheckCodeDto", value = "id与code")
                                                @RequestBody AccPermissionCheckCodeDto accPermissionCheckCodeDto) {
        logger.info("校验权限编码唯一性 uacActionCheckCodeDto={}", accPermissionCheckCodeDto);

        Long id = accPermissionCheckCodeDto.getPermissionId();
        String permissionCode = accPermissionCheckCodeDto.getPermissionCode();

        Example example = new Example(AccPermission.class);
        Example.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andNotEqualTo("permissionId", id);
        }
        criteria.andEqualTo("permissionCode", permissionCode);

        int result = accPermissionService.selectCountByExample(example);
        return WrapMapper.ok(result < 1);
    }

    /**
     * 检测权限名称是否已存在
     *
     * @param accPermissionCheckNameDto the acc permission check code dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkName")
    @ApiOperation(httpMethod = "POST", value = "检测权限名称是否已存在")
    public Wrapper<Boolean> checkPermissionName(@ApiParam(name = "accPermissionCheckNameDto", value = "id与name")
                                            @RequestBody AccPermissionCheckNameDto accPermissionCheckNameDto) {
        logger.info("校验权限名称唯一性 accPermissionCheckNameDto={}", accPermissionCheckNameDto);

        Long id = accPermissionCheckNameDto.getPermissionId();
        String permissionName = accPermissionCheckNameDto.getPermissionName();

        Example example = new Example(AccPermission.class);
        Example.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andNotEqualTo("permissionId", id);
        }
        criteria.andEqualTo("permissionName", permissionName);

        int result = accPermissionService.selectCountByExample(example);
        return WrapMapper.ok(result < 1);
    }

    /**
     * 检测权限URL及请求方法唯一性
     *
     * @param accPermissionCheckUrlAndTypeDto the uac action check url dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkUrlAndType")
    @ApiOperation(httpMethod = "POST", value = "检测权限URL及请求方法唯一性")
    public Wrapper<Boolean> checkActionUrlAndType(@ApiParam(name = "uacActionCheckUrlDto", value = "id与url,type")
                                                      @RequestBody AccPermissionCheckUrlAndTypeDto accPermissionCheckUrlAndTypeDto) {
        logger.info("检测权限URL唯一性 accPermissionCheckUrlAndTypeDto={}", accPermissionCheckUrlAndTypeDto);

        Long id = accPermissionCheckUrlAndTypeDto.getPermissionId();
        String url = accPermissionCheckUrlAndTypeDto.getUrl();
        String permissionType = accPermissionCheckUrlAndTypeDto.getPermissionType();

        Example example = new Example(AccPermission.class);
        Example.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andNotEqualTo("permissionId", id);
        }
        criteria.andEqualTo("url", url);
        criteria.andEqualTo("permissionType", permissionType);

        int result = accPermissionService.selectCountByExample(example);
        return WrapMapper.ok(result < 1);
    }
}
